package com.hahaliu.hahaliu.controller.user;

import com.hahaliu.hahaliu.common.exception.BaseException;
import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.common.utils.RedisUtil;
import com.hahaliu.hahaliu.common.context.CurrentUser;
import com.hahaliu.hahaliu.pojo.entity.CommentTree;
import com.hahaliu.hahaliu.pojo.vo.GetCommentVO;
import com.hahaliu.hahaliu.service.comment.CommentService;
import com.hahaliu.hahaliu.service.user.UserCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/comment")
@Api(tags = "用户端评论相关接口")
@Slf4j
public class CommentController {
	@Autowired
	private CommentService commentService;
	@Autowired
	private CurrentUser currentUser;
	@Autowired
	private UserCommentService userCommentService;
	@Autowired
	private RedisUtil redisUtil;

	@GetMapping("/list")
	@ApiOperation("获取评论列表")
	public Result<GetCommentVO> getCommentTreeByVid(@RequestParam("vid") Integer vid,
	                                                @RequestParam("offset") Long offset,
	                                                @RequestParam("type") Integer type) {
		GetCommentVO getCommentVO = new GetCommentVO();
		long count = redisUtil.zCard("comment_video:" + vid);
		Map<String, Object> map = new HashMap<>();
		if (offset >= count) {
			// 已经获取到全部根评论了，直接返回空list
			getCommentVO.setMore(false);
			getCommentVO.setCommentTreeList(Collections.emptyList());
		} else if (offset + 10 >= count){
			// 表示这次查询会查完全部根评论
			getCommentVO.setMore(false);
			getCommentVO.setCommentTreeList(commentService.getCommentTreeByVid(vid, offset, type));
		} else {
			// 表示还有很多评论没查到
			getCommentVO.setMore(true);
			getCommentVO.setCommentTreeList(commentService.getCommentTreeByVid(vid, offset, type));
		}
		return Result.success(getCommentVO);
	}

	@PostMapping("/add")
	@ApiOperation("发表评论")
	public Result<CommentTree> addComment(
			@RequestParam("vid") Integer vid,
			@RequestParam("root_id") Integer rootId,
			@RequestParam("parent_id") Integer parentId,
			@RequestParam("to_user_id") Integer toUserId,
			@RequestParam("content") String content) {
		Integer uid = currentUser.getUserId();
		CommentTree commentTree = commentService.sendComment(vid, uid, rootId, parentId, toUserId, content);
		if (commentTree == null) {
			throw new BaseException("发送失败");
		}
		return Result.success(commentTree);
	}

	/**
	 * 删除评论
	 * @param id 评论id
	 * @return  响应对象
	 */
	@PostMapping("/delete")
	@ApiOperation("删除评论")
	public Result delComment(@RequestParam("id") Integer id) {
		Integer loginUid = currentUser.getUserId();
		commentService.deleteComment(id, loginUid, currentUser.isAdmin());
		return Result.success();
	}

	/**
	 * 获取用户点赞点踩评论集合
	 */
	@GetMapping("/comment/get-like-and-dislike")
	public Result<Map<String, Object>> getLikeAndDislike() {
		Integer uid = currentUser.getUserId();

		Map<String, Object> userLikeAndDislike = userCommentService.getUserLikeAndDislike(uid);

		return Result.success(userLikeAndDislike);
	}


}
